package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public class DataActionColor extends DataAction implements ITag {
	public static String DEFAULT_ID = "_ActionColor";

	DataColor startColor = new DataColor();
	DataColor color = new DataColor();
	DataColor endColor = new DataColor();

public DataActionColor(DataNode parent) {
	super(parent);

	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch
}
public void buildFromgrfxML(XmlElement xml)
{
	String name = xml.getAttributeValueByName(Token.ID);
	if(name!=null)
	{
		try{
			setId(name,this);
		}
		catch(LockException le){} //Dummy catch
	}


	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.START_COLOR))
		{
			startColor = new DataColor();
	  		startColor.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.COLOR))
		{
			color = new DataColor();
			color.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.END_COLOR))
		{
			endColor = new DataColor();
			endColor.buildFromgrfxML(element);
		}
		else if(super.buildFromElement(element)==false && !NameHelper.isSystemName(element.getName()))
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.ACTION_COLOR,""+element.getStartLineNo()} );
		}
	}
}
public void copyFromgrfxML(XmlElement xml)
{
	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.START_COLOR))
		{
			startColor.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.COLOR))
		{
			color.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.END_COLOR))
		{
			endColor.copyFromgrfxML(element);
		}
		else if(super.buildFromElement(element)==false)
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.ACTION_COLOR,""+element.getStartLineNo()} );
		}
	}
}
public DataAction getClone() {


	DataActionColor newActionColor = new DataActionColor(parent);

	super.getClonedElements(newActionColor);

	newActionColor.startColor = startColor.getClone();
	newActionColor.color = color.getClone();
	newActionColor.endColor = endColor.getClone();

	return newActionColor;
}
public DataAction getClone(String path) {


	DataActionColor newActionColor = new DataActionColor(parent);

	GrfxMLStoreProcessor.setCloneName( path,this,newActionColor );

	super.getClonedElements(path,newActionColor);

	newActionColor.startColor = startColor.getClone(path);
	newActionColor.color = color.getClone(path);
	newActionColor.endColor = endColor.getClone(path);

	return newActionColor;
}
public DataColor getColor() {
	return color;
}
public DataColor getEndColor() {
	return endColor;
}
public DataColor getStartColor() {
	return startColor;
}
/**
 * process method comment.
 */
protected void process()
{
 try{
	int newX = startColor.getValueX();
	int diffX = endColor.getValueX() - newX;
	newX += currentStep.getValue() * diffX / steps.getValue();
	color.setValueX(newX, this);

	int newY = startColor.getValueY();
	int diffY = endColor.getValueY() - newY;
	newY += currentStep.getValue() * diffY / steps.getValue();
	color.setValueY(newY, this);

	int newZ = startColor.getValueZ();
	int diffZ = endColor.getValueZ() - newZ;
	newZ += currentStep.getValue() * diffZ / steps.getValue();
	color.setValueZ(newZ, this);

	int newT = startColor.getValueT();
	int diffT = endColor.getValueT() - newT;
	newT += currentStep.getValue() * diffT / steps.getValue();
	color.setValueT(newT, this);

 }catch(LockException le){}
}
public void register(DataLinker store) {

	super.register(store);

	try{
		store.add(startColor,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(color,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(endColor,this);
	}
	catch(LockException le)
	{}//Dummy catch
}
public void replace(Field oldObj, Field newObj) {

	replace( oldObj, newObj,true);

}
public void replace(Field oldObj, Field newObj, boolean recursive) {

	super.replace(oldObj,newObj);

	if(startColor==oldObj)
		startColor=(DataColor)newObj;

	if(color==oldObj)
		color=(DataColor)newObj;

	if(endColor==oldObj)
		endColor=(DataColor)newObj;
}
public void resetChanged() {
	super.resetChanged();
	startColor.resetChanged();
	color.resetChanged();
	endColor.resetChanged();
}
protected void setColor(DataColor newColor) {
	color = newColor;
}
protected void setEndColor(DataColor newEndColor) {
	endColor = newEndColor;
}
protected void setStartColor(DataColor newStartColor) {
	startColor = newStartColor;
}
public String togrfxML(String name) {
	String xml = "<" + name;

	if(!NameHelper.isSystemName(getId()))
	{
		xml += " id=\""+getId()+"\"";
	}

	xml += ">";
	xml += super.togrfxML(name);
	xml += startColor.togrfxML("startColor");
	xml += color.togrfxML("color");
	xml += endColor.togrfxML("endColor");
	xml += "</" + name + ">\n";
	return xml;
}
public String toString()
{
	if(GrfxMLEngine.DEBUG)
		return(togrfxML( DEFAULT_ID.substring(1) ));

	String xml = "<"+DEFAULT_ID.substring(1)+"/>";

	return xml;
}
}
